import {Component, ViewChild} from '@angular/core';
import {OrganizationEditComponent} from "../edit/organization-edit.component";
import {OrganizationTreeComponent} from "../tree/organization-tree.component";
import {GnTable} from "geminis-ng";
import {GnTableColumn} from "geminis-ng/data/table.component";

@Component({
  selector: 'gn-resources-organization-list',
  templateUrl: './organization-list.component.html'
})
export class OrganizationListComponent extends GnTable {

  typeEdit = OrganizationEditComponent;

  columns: GnTableColumn[] = [
    {label: '名称', field: 'name'},
    {label: '类型', field: 'type'},
    {label: '显示顺序', field: 'showOrder'}
  ];

  @ViewChild(OrganizationTreeComponent, {static: true})
  tree: OrganizationTreeComponent;

  refreshData(): void {
    this.table.cleanSelectionAndLoadData();
    this.tree.tree.reloadCurrent();
  }

  refreshTable(): void {
    let selectedId = null;
    if (this.tree.selectedData.id != '') {
      selectedId = this.tree.selectedData.id;
    }
    this.table.filterGroup = {
      type: 'and',
      childGroups: [],
      filters: [
        {
          field: 'parentId',
          compareType: selectedId === null ? 'is null' : '=',
          value: selectedId === null ? 'null' : selectedId
        }
      ]
    }
    this.table.cleanSelectionAndLoadData();
  }

  delete(): void {
    this.table.deleteData(this.table.url, this.table.selection.selected.map(v => v.pathId));
  }
}

